package selectsort;

public class Select {
    public void sort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            if (minIndex != i) swap( arr, minIndex, i);
        }
    }

    private void swap(int[] arr, int minIndex, int i) {
        int temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }

    public void twoWaySort(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left <right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if (arr[i] < arr[minIndex]) {
                    minIndex = i;
                }

                if (arr[i] > arr[maxIndex]) maxIndex = i;
            }

            swap(arr,minIndex, left);
            if (maxIndex == left) {
                maxIndex =  minIndex;
            }
            swap(arr, maxIndex, right);
            left++;
            right--;
        }

    }
}
